(function() {
	'use strict';
	angular.module('services.workOrderData.auditData')
		.factory('NCRateService', NCRateService);
	function NCRateService(Restangular,CoreService){
		return {
			query: query,
			fmtSendData: fmtSendData,
			getName: getName,
			getEchartsData: getEchartsData

		};
		//请求方法
		function query(params,callback){
			Restangular.all('sas/workOrderData/NCPercentage/query').post(params).then(callback);
		}
		//格式化需求发送给后端的字段
	    function fmtSendData(sendData){
	      var send = {
	        startTime:'',
	        endTime: '',
	        statisticsType:''
	      };
	      send.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd');
	      send.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd');
	      send.statisticsType = sendData.statisticsType;
	      send.timeType = sendData.timeType;
	      return send;
	    }
	    //根据统计维度code获取所对应的名字
	    function getName(code,statisticsTypes){
	      var name = '';
	      if(code){
	        angular.forEach(statisticsTypes,function(res){
	          if(res.statisticsType === code){
	            name = res.statisticsTypeName;
	          }
	        });
	      }
	      return name;
	    }
	    function getEchartsData(items,title){
			var temp = {
				xData:[],
				legendName:['严重NC比例','非严重NC比例'],
				NCPercentage:[],
				notNCPercentage:[]
			};
			angular.forEach(items,function(item){
				temp.xData.push(item.workOrderType);
				temp.notNCPercentage.push(item.notNCPercentage.substring(0,item.notNCPercentage.length-1));
				temp.NCPercentage.push(item.ncPercentage.substring(0,item.ncPercentage.length-1));
			});
		return {
	        title: {
	          text: title,
	          x: 'center'
	        },
	        animation: false,
	        tooltip: {
	        	trigger: 'axis',
	        	 axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        }
	        },
	        grid: {
	          x2: 170
	        },
	        legend: {
	          data: temp.legendName,
	          orient: 'vertical',
	          x: 'right',
	          y: 'center'
	        },
	        xAxis: [{
	          type: 'category',
	          data: temp.xData
	        }],
	        yAxis: [
	          { type: 'value',axisLabel : {formatter: '{value} %'},min:'0',max:'120',splitNumber:'6'}
	        ],
	        series: [
	        	{name:'非严重NC比例',type:'bar',stack: '比例',data:temp.notNCPercentage},
	        	{name:'严重NC比例',type:'bar',stack: '比例',itemStyle : { normal: {label : {show: true,textStyle: {color: 'black' }, position: 'inside',formatter : "{c}%"}}},data:temp.NCPercentage}
	        ]
	      };
		}

 }
})();